﻿using CommandProcessor.Command;
using Data.Infrastructure;
using Data.Models;
using Data.Repositories;
using Domain.Commands;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Domain.Handlers
{
    public class CreateOrUpdateActualGoodInStockHandler : ICommandHandler<CreateOrUpdateActualGoodInStockCommand>
    {
        private readonly IActualGoodInStockRepository actualGoodInStockRepository;
        private readonly IUnitOfWork unitOfWork;
        public CreateOrUpdateActualGoodInStockHandler(IActualGoodInStockRepository actualGoodInStockRepository, IUnitOfWork unitOfWork)
        {
            this.actualGoodInStockRepository = actualGoodInStockRepository;
            this.unitOfWork = unitOfWork;
        }
        public ICommandResult Execute(CreateOrUpdateActualGoodInStockCommand command)
        {
            var actualGoodInStock = new ActualGoodInStock
            {
                ID = command.ID,
                CompanyID=command.CompanyID,
                DateIn=command.DateIn,
                GoodID=command.GoodID,
                InputPrice=command.InputPrice,
                InputQuantity=command.InputQuantity,
                OutputQuantity=command.OutputQuantity,
                RemainQuantity=command.RemainQuantity,
                StockID=command.StockID,
                UOMID=command.UOMID
            };
            if (actualGoodInStock.ID == 0)
                actualGoodInStockRepository.Add(actualGoodInStock);
            else
                actualGoodInStockRepository.Update(actualGoodInStock);

            unitOfWork.Commit();
            return new CommandResult(true);
        }
        public ICommandResult ExecuteRange(IEnumerable<CreateOrUpdateActualGoodInStockCommand> commands)
        {
            throw new NotImplementedException();
        }
        public ICommandResult DeleteRange(IEnumerable<CreateOrUpdateActualGoodInStockCommand> commands)
        {
            throw new NotImplementedException();
        }
    }
}
